package io.github.wslxm.springbootplus2.manage.gc.generate.gcimpl;

import com.alibaba.fastjson.JSON;
import io.github.wslxm.springbootplus2.manage.gc.config.GcConfig;
import io.github.wslxm.springbootplus2.manage.gc.constant.TpParamConstant;
import io.github.wslxm.springbootplus2.manage.gc.constant.VueVersionConstant;
import io.github.wslxm.springbootplus2.manage.gc.generate.util.GenerateUtil;
import io.github.wslxm.springbootplus2.manage.gc.generate.util.GenerateVue2Util;
import io.github.wslxm.springbootplus2.manage.gc.generate.util.GenerateVue3Util;
import io.github.wslxm.springbootplus2.manage.gc.model.po.DbFieldPO;
import io.github.wslxm.springbootplus2.manage.gc.generate.GcSevice;
import io.github.wslxm.springbootplus2.manage.gc.utils.GcDataUtil;
import io.github.wslxm.springbootplus2.manage.gc.utils.GcFileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

@SuppressWarnings("all")
@Component
@Slf4j
public class GcVueMain extends BaseGcImpl implements GcSevice {

    /**
     * 模板key
     */
    public static final String KEY_NAME = "X-Vue";


    /**
     * 生成Html-main 主页
     *
     * @param data    数据
     * @param GenerateConfig 数据
     * @param path    生成代码路径
     * @return void
     * @date 2019/11/20 19:18
     */
    @Override
    public void run(GcConfig gcConfig) {
        log.info("开始生成: {}", KEY_NAME);
        // 数据表格字段
        StringBuffer vueInfoColumns = new StringBuffer(" ");
        StringBuffer vueInfoColumnSlots = new StringBuffer(" ");
        List<DbFieldPO> dbFields = gcConfig.getDbFields();
        for (DbFieldPO dbFieldPO : dbFields) {
            // 未勾选的字段过滤
            if (!GenerateUtil.isChecked(dbFieldPO)) {
                continue;
            }
            String vueFieldType = dbFieldPO.getVueFieldType() + "";
            // 指定类型字段不生成到列表中，同时排除list接口查询
            List<Integer> vueFieldTypeArray = JSON.parseObject(gcConfig.getDefaultTemplateParam(TpParamConstant.VUE_FIELD_TYPES_ARRAY), List.class);
            if (vueFieldTypeArray.contains(vueFieldType)) {
                continue;
            }

            String name = GcDataUtil.getFieldName(gcConfig, dbFieldPO.getName());
            String newDesc = GenerateUtil.getDesc(dbFieldPO.getDesc(), dbFieldPO.getName());
            boolean isSearch = dbFieldPO.getIsSearch() == null ? false : dbFieldPO.getIsSearch();
            Integer vueFieldTypeInt = (vueFieldType != null) ? Integer.parseInt(vueFieldType.toString()) : -1;
            // 平台字段  // 插槽字段处理(搜索或特殊展示)
            if (VueVersionConstant.VUE3.equals(gcConfig.getVueVersion())) {
                GenerateVue3Util.vue3InfoColumns(gcConfig, vueFieldTypeInt, vueInfoColumns, newDesc, name, isSearch, dbFieldPO.getDictCode());
                vueInfoColumnSlots.append(GenerateVue3Util.jxVue3MainColumnsSlot(vueFieldTypeInt, name, isSearch));
            } else {
                GenerateVue2Util.vue2InfoColumns(gcConfig, vueFieldTypeInt, vueInfoColumns, newDesc, name, isSearch, dbFieldPO.getDictCode());
                vueInfoColumnSlots.append(GenerateVue2Util.jxVue2MainColumnsSlot(vueFieldTypeInt, name, isSearch));
            }
        }
        // 数据保存
        gcConfig.setTemplateParam("vueInfoColumns", vueInfoColumns.toString());
        gcConfig.setTemplateParam("vueInfoColumnSlots", vueInfoColumnSlots.toString());
        // 开始生成文件并进行数据替换
        GcFileUtil.replacBrBwWritee(gcConfig, GcFileUtil.getBrBwPath(gcConfig, KEY_NAME));
    }


}
